<template>
  <div class="permission">
    <el-card class="topCrad">
      <div class="rigth">
        <el-button type="primary" size="mini" @click="addPermission">
          添加权限</el-button>
      </div>
    </el-card>
    <el-table border :data="list" row-key="id">
      <el-table-column label="名称" prop="name" />
      <el-table-column label="标识" prop="code" />
      <el-table-column label="描述" prop="description" />
      <el-table-column label="操作">
        <template v-slot="{ row }">
          <el-button type="text" @click="addEven(row.id)">添加</el-button>
          <el-button type="text" @click="editEvent(row)">编辑</el-button>
          <el-button type="text" @click="del(row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <add ref="add" @getData="getData" />
  </div>
</template>

<script>
import { sysPermission, sysPermissionDelete } from '@/api/permission'
import Add from './component/add.vue'
export default {
  components: {
    Add
  },
  data() {
    return {
      list: []
    }
  },
  created() {
    this.getData()
  },
  methods: {
    async getData() {
      const { data: res } = await sysPermission()
      this.list = this.changeData(res, '0')
    },
    changeData(arr, pid) {
      return arr.filter((item) => {
        if (item.pid === pid) {
          item.children = this.changeData(arr, item.id)
          return true
        }
      })
    },
    // 一级添加事件
    addPermission() {
      this.$refs.add.isshow = true
      this.$refs.add.mode = 'add'
    },
    // 下面添加事件
    addEven(id) {
      this.$refs.add.isshow = true
      this.$refs.add.form.type = 2
      this.$refs.add.form.pid = id
      this.$refs.add.mode = 'add'
    },
    // 编辑事件
    editEvent(row) {
      this.$refs.add.mode = 'edit'
      this.$refs.add.isshow = true
      this.$refs.add.form = { ...row }
    },
    // 删除事件
    // 删除点击事件
    del(id) {
      this.$confirm('您确定删除该项吗?', '提示')
        .then(async() => {
          await sysPermissionDelete(id)
          this.$message.success('删除成功')
          this.getData()
        })
        .catch(() => {})
    }
  }
}
</script>

<style lang="scss" scoped>
.permission {
  padding: 20px;
  .topCrad {
    margin-bottom: 20px;
    .rigth {
      text-align: right;
    }
  }
}
</style>
